/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package modelo;

/**
 *
 * @author DAHER
 */
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JOptionPane;


public class Conector {

    private static Connection conexion; 
    private static Statement sentencia;
    private static String driver = "org.postgresql.Driver";
    private static String con = "jdbc:postgresql://localhost:5432/colegio";
    private static String user = "sis2";
    private static String pas = "informatica";
    
    public static void  conexion()
    {
        abrirConexion(driver, con, user, pas); 
       
                
    }
    public static Connection getConexion(){
        return conexion;
    }
    
    public static boolean abrirConexion(String driver,String con,String user,String pas)
    {
       boolean res = true;
       
       try
       {
            Class.forName(driver);
            conexion = DriverManager.getConnection(con,user,pas);
            sentencia = conexion.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
       }
       catch ( Exception e )
       {
            JOptionPane.showMessageDialog(null, "No se pudo cargar el puente JDBC-ODBC.");
            System.out.println("Error al conectar: " + e.getMessage());
            res = false;
       }
       return res;
    }

    public static boolean cerrarConexion()
    {
        boolean res = true;
        try
	{
            sentencia.close();
            conexion.close();
        }
        catch(Exception e)
	{
            System.out.println("Error al cerrar la conexion: " + e.getMessage());
            res = false;
        }
        return res;
    }
    public static ResultSet consultar(String consulta)
    {
        boolean aux = false;
        ResultSet res = null;
        conexion();
      try
      {
          aux = true;
          res = sentencia.executeQuery(consulta);
            
      }
      catch(Exception e)
      {
            System.out.println(e);
      }        
      return res; 
    }

}
